Skip to content

gh-145633: deprecate float.__getformat__() class method#146400

Open
skirpichev wants to merge 4 commits intopython:mainfrom
skirpichev:deprecate-__getformat__/145633
Open

gh-145633: deprecate float.__getformat__() class method#146400
skirpichev wants to merge 4 commits intopython:mainfrom
skirpichev:deprecate-__getformat__/145633

Conversation

@skirpichev
Copy link
Copy Markdown
Member

@skirpichev skirpichev commented Mar 25, 2026

(Contributed by Hugo van Kemenade and Stan Ulbrych in :gh:`76007`.)

* The ``__getformat__()`` class method of the :class:`float` is deprecated
and will be removed in Python 3.20.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should document the deprecation in What's New in Python 3.15.

What is the migration path for projects currently relying on __getformat__()? Do they have to copy/paste the big _have_ieee_doubles() function?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the migration path for projects currently relying on getformat()? Do they have to copy/paste the big _have_ieee_doubles() function?

Primary goal is using the CPython's test suite. No changes needed.

But for the rest - yes.

Copy link
Copy Markdown
Member Author

@skirpichev skirpichev Mar 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can simplify helper? I.e. no ctypes.

In float_info docs the C99, section 5.2.4.2.2 was referenced for "details":

The characteristics of floating types are defined in terms of a model that describes a representation of floating-point numbers and values that provide information about an implementation’s floating-point arithmetic. ()
(
) The floating-point model is intended to clarify the description of each floating-point characteristic and does not require the floating-point arithmetic of the implementation to be identical.

The model is same as in IEEE specification (modulo different summation limits) and we can check that implementation has same set of values for finite numbers, including subnormals and two zeros. And check that there are two infinities and a nan. I.e. slightly extended "if" statement. (The C11 standard has additionally DBL_HAS_SUBNORM macro. I think we could update float_info with this macro, it will be helpful in such check. Edit: it's obsoleted in C23.)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still don't understand how to update code using __getformat__()? Should code now assume that Python always support IEEE 754 and so what only matters is the endian? In that case, you should point to sys.byteorder.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The __getformat__() now is actual only for alternative implementations. They can override this and skip some parts of the CPython test suite (not all in fact related just to specific format of floating-point numbers, but some also expect certain runtime behavior from arithmetic operations).

The _have_ieee_doubles() use some heuristics to check that some unknown alternative Python implementation actually uses IEEE doubles for the float type, at least can encode all stuff. Old __getformat__() just checked one finite number (like we do now with the ctypes codepath in helper). But instead we also could check parameters for floating-point type, presence of infinities/nans/subnormals. I'm not sure which heuristics is better nowadays, but the ctypes module is missing on some platforms.

@skirpichev skirpichev requested a review from AA-Turner as a code owner March 25, 2026 07:09
@skirpichev skirpichev requested a review from vstinner March 27, 2026 02:03
# for a discussion of this number.
SOCK_MAX_SIZE = 16 * 1024 * 1024 + 1

def _have_ieee_doubles():
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand well the purpose of this function. Python now requires IEEE 754 as a build dependency: https://docs.python.org/dev/using/configure.html#build-requirements. Is this test to actually check if it's the case?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is only for alternative Python implementations, using the CPython test suite.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants